package com.ysx;

import com.alibaba.fastjson.JSON;
import com.ysx.article.feign.ApArticleFeign;
import com.ysx.article.pojo.ApArticle;
import com.ysx.search.document.ArticleInfoDocument;
import com.ysx.search.repository.ArticleInfoDocumentRepository;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

import java.util.List;

/**
* @author ljh
* @version 1.0
* @date 2021/5/2 11:36
* @description 标题
* @package
*/
@SpringBootApplication
@MapperScan(basePackages = "com.ysx.search.mapper")//扫描mapper接口所在的包即可
@EnableDiscoveryClient//启用注册与发现
@EnableFeignClients(basePackages = "com.ysx.*.feign")
public class SearchApplication implements InitializingBean {

    @Autowired
    private ApArticleFeign apArticleFeign;

    @Autowired
    private ArticleInfoDocumentRepository articleInfoRepository;

    public static void main(String[] args) {
        SpringApplication.run(SearchApplication.class,args);
    }

    /**
     * 在启动类初始化的时候 启动的这个方法
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        List<ApArticle> data = apArticleFeign.findAll().getData();
        if(data!=null){
            List<ArticleInfoDocument> articleInfos = JSON.parseArray(JSON.toJSONString(data), ArticleInfoDocument.class);
            articleInfoRepository.saveAll(articleInfos);
        }
    }


    //这个方法也可以 相当于 init-method
    /*@PostConstruct
    public void  importEsData(){

    }*/


}


